$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/indeximg/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '跳转链接或页面', name: 'hrefurl', index: 'hrefurl', width: 80 },
            { label: '类型', name: 'type', width: 80, formatter: function(value, options, row){
                    return value === 0 ?
                        '<span class="label label-danger">跳转链接</span>' :
                        '<span class="label label-success">跳转页面</span>';
                }},
            { label: '图片地址', name: 'img', index: 'img', width: 80 , formatter:function(value, options, row) {
                    return '<img  style="width: 50px" src="' + value + '">';
                }},
			{ label: '添加时间', name: 'createtime', index: 'createtime', width: 80 },
            { label: '操作', name: 'sy', width: 100,align:'center',formatter: function(cellvalue, options, rowdata) {
                    return "<a style='cursor:pointer;' onclick='vm.update("+ rowdata.id +")'>" +
                        "<span class='btn btn-primary'>修改</span></a>&nbsp;&nbsp;&nbsp;&nbsp;" ;
                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        fileList1: [],
		showList: true,
		title: null,
		indeximg: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.indeximg = {};


            vm.fileList1 = [];

		},
		update: function (id) {
            vm.fileList1 = [];
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.indeximg.id == null ? "sys/indeximg/save" : "sys/indeximg/update";
                vm.indeximg.img = '';
                if(vm.fileList1.length>0){
                    vm.indeximg.img = vm.fileList1[0].url;
                }
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.indeximg),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/indeximg/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
        handleRemove: function(file, fileList) {
            console.log(fileList);
            if(fileList.length>0){
                vm.fileList1 = [];
                for(var i=0;i<fileList.length;i++){
                    vm.fileList1.push({name: fileList[i].name,url: fileList[i].url});

                }
            }
            console.log(vm.fileList1);

        },
        handlePreview: function(file) {
            console.log(file);
        },

        handsuccess: function(response, file, fileList) {
            vm.fileList1.push({name: response.url,url: response.url});

        },
		getInfo: function(id){
			$.get(baseURL + "sys/indeximg/info/"+id, function(r){
                vm.indeximg = r.indeximg;
                vm.fileList1 = [];
                if(r.indeximg.img!=null&&r.indeximg.img!=''){
                    vm.fileList1.push({name: r.indeximg.img,url: r.indeximg.img});
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});